﻿using DLog.Access;
using DLog.Common;
using DLog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLog
{
    public class Logger : ILogger
    {
        protected IDal Dal;

        public void Init(string target, string addition)
        {
            Dal = DalFactory.GetDal(target, addition);
        }

        public bool Trace(string message)
        {
            return Log(message, (int)EnumLogLevel.Trace);
        }

        public bool Trace(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Trace);
        }

        public bool Trace(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Trace);
        }

        public bool Debug(string message)
        {
            return Log(message, (int)EnumLogLevel.Debug);
        }

        public bool Debug(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Debug);
        }

        public bool Debug(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Debug);
        }

        public bool Info(string message)
        {
            return Log(message, (int)EnumLogLevel.Info);
        }

        public bool Info(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Info);
        }

        public bool Info(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Info);
        }

        public bool Warn(string message)
        {
            return Log(message, (int)EnumLogLevel.Warn);
        }

        public bool Warn(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Warn);
        }

        public bool Warn(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Warn);
        }

        public bool Error(string message)
        {
            return Log(message, (int)EnumLogLevel.Error);
        }

        public bool Error(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Error);
        }

        public bool Error(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Error);
        }

        public bool Fatal(string message)
        {
            return Log(message, (int)EnumLogLevel.Fatal);
        }

        public bool Fatal(Exception ex)
        {
            return Log(ex, (int)EnumLogLevel.Fatal);
        }

        public bool Fatal(string message, Exception ex)
        {
            return Log(message, ex, (int)EnumLogLevel.Fatal);
        }

        public virtual bool Log(string meesage, int logLevel)
        {
            LogModel lm = new Model.LogModel() {
                LogId = KeyGenerator.GetKey(),
                CreateDate=DateTime.Now,
                LogLevel=logLevel,
                Message=meesage,
                StackTrade = string.Empty,
                Origin = string.Empty
            };
            return Dal.Insert(lm)>0;
        }

        public virtual bool Log(Exception ex, int logLevel)
        {
            LogModel lm = new Model.LogModel()
            {
                LogId = KeyGenerator.GetKey(),
                CreateDate = DateTime.Now,
                LogLevel = logLevel,
                Message = ex.Message,
                StackTrade=ex.StackTrace,
                Origin=ex.Source
            };
            return Dal.Insert(lm)>0;
        }

        public virtual bool Log(string meesage, Exception ex, int logLevel)
        {
            LogModel lm = new Model.LogModel()
            {
                LogId = KeyGenerator.GetKey(),
                CreateDate = DateTime.Now,
                LogLevel = logLevel,
                Message = meesage+"\t\nException:"+ex.Message,
                StackTrade = ex.StackTrace,
                Origin = ex.Source
            };
            return Dal.Insert(lm)>0;
        }

        public virtual bool Log(LogModel lm)
        {
            return Dal.Insert(lm) > 0;
        }

        public LogModel GetLog(string logId)
        {
            return Dal.SelectModel(logId);
        }

        public List<LogModel> GetLogs()
        {
            return Dal.SelectList();
        }

        public List<LogModel> GetLogs(int pageIndex, int pageSize, out int totalCount)
        {
            return Dal.SelectList(pageIndex,  pageSize, out totalCount);
        }

        public List<LogModel> GetLogs(int logLevel, int pageIndex, int pageSize,out int totalCount)
        {
            return Dal.SelectList(logLevel, pageIndex, pageSize, out totalCount);
        }

        public List<LogModel> GetLogs(DateTime createStartDate, DateTime createEndDate, int pageIndex, int pageSize, out int totalCount)
        {
            return Dal.SelectList(createStartDate, createEndDate, pageIndex, pageSize, out totalCount);
        }

        public List<LogModel> GetLogs(int logLevel, DateTime createStartDate, DateTime createEndDate, int pageIndex, int pageSize, out int totalCount)
        {
            return Dal.SelectList(logLevel, createStartDate, createEndDate, pageIndex, pageSize, out totalCount);
        }

        public bool DeleteModel(string logId)
        {
            return Dal.Delete(logId)>0;
        }
        public bool DeleteList(List<string> logIds)
        {
            bool result=true;
            foreach (string logId in logIds)
            {
                 result=result&&(Dal.Delete(logId)>0);
            }
            return result;
        }
    }
}
